/**
 * Created by Administrator on 2017/5/18.
 */
 var linkUrl = "http://"+window.location.host+"/";//获取当前域名
$(function () {
    $.ajax({
        type : "get",
        url : linkUrl + "isLoged",
        data: {},
        dataType: 'json',
        success:function (Result) {
            var result = Result.result;
            if(result==1){
                $(".pcc_gerenbnt").removeClass("hide");
                $("#to-login").addClass("hide");
                $("#to-reg").addClass("hide");
            }
        }
    });
//	点击回到头部
	$("#sidebar-c").click(function(){
        $('body,html').animate({scrollTop:0},1000);
        return false;
    });
	
    $("#pcc_yzmbtn2").click(function () {
        $.ajax({
            type: 'POST',
            url : linkUrl + "sendCodeMessage",
            data: {mobile : $('#pcc_phone2').val(),type : "change"},
            dataType: 'json',
            error:function (){alert("error");},
            success:function (Result) {
                //倒计时代码
            }
        })
    })
	$("#pcc-logout").click(function () {
	    $.ajax({
	        type : "post",
	        url : linkUrl + "logout",
	        data: {},
	        dataType: 'json',
	        success:function (Result) {
	            window.location.href="index.html";
	        }
	    })
	});

	getusermessage();
	issign();
// 改密码
	
	$('#pcc_passworder').blur(function(){
		var i=$('#pcc_passworder');
		pcc_passworder(i);
	})
	$('#pcc_phone').blur(function(){
		var i=$('#pcc_phone');
		pcc_phone(i);
	})
	$('#pcc_redpassworder').blur(function(){
		var i=$('#pcc_redpassworder');
		pcc_passworder(i);
	})
	$('#pcc_againpassworder').blur(function(){
		var i=$('#pcc_againpassworder');
		pcc_passworder(i);
		if ($('#pcc_againpassworder').val()!=$('#pcc_passworder').val()) {
			$('#pcc_againpassworder').next().removeClass('hide');
			return false;
		}
	})
	// 改信息
	var truenameflag = false;
	$('#pcc_truename').blur(function(){
		var i=$('#pcc_truename');
        truenameflag = pcc_name(i);
	})
	var emailflag = false;
	$('#pcc_email').blur(function(){
		var i=$('#pcc_email');
        emailflag = pcc_email(i);
	})
	var addressflag = false;
	$('#pcc_address').blur(function(){
		var i=$('#pcc_address');
        addressflag = pcc_name(i);
	})
	$("#pcc_xinxi").click(function () {
			$.ajax({
				type:"post",
				url:linkUrl + "modifyUserInfo",
				data:{realName:$('#pcc_truename').val(),email:$('#pcc_email').val(),address:$('#pcc_address').val()},
				dataType:"json",
				success:function (Result) {
					if(Result.result==1){
						getusermessage();
					}else{
						alert(Result.message);
					}
                }
			})	
    })
    $("#pcc_qiandaobnt").click(function () {
			$.ajax({
				type:"post",
				url:linkUrl + "insertQiandao",
				data:{content:$('#pcc_textarea').val()},
				dataType:"json",
				success:function (Result) {
					if(Result.result==1){
						$('#Modal2').modal('hide');
						  issign();		  
					}else{
						alert(Result.message);
					}
                }
			})	
    })

	// 改手机号码
	$('#pcc_name2').blur(function(){
		var i=$('#pcc_name2');
		pcc_name(i);
	})
	$('#pcc_passworder2').blur(function(){
		var i=$('#pcc_passworder2');
		pcc_passworder(i);
	})
	$('#pcc_phone2').blur(function(){
		var i=$('#pcc_phone2');
		pcc_phone(i);

	})
	$('#pcc_redpassworder2').blur(function(){
		var i=$('#pcc_redpassworder2');
		pcc_passworder(i);
	})
	
	$("#pcc_yzmbtn").click(function () {
	$.ajax({
        type: 'POST',
        url : linkUrl + "sendCodeMessage",
        data: {mobile : $('#pcc_phone').val(),type : "modify"},
        dataType: 'json',
        async:false,
        error:function (){alert("error");},
        success:function (Result) {
            //倒计时代码
        }
	  })
   })


	// 页面加载完毕
 
})

function getusermessage(){
    	$.ajax({
            type:"get",
            url:linkUrl + "getUserInfo",
            data:{},
            dataType:"json",
            success:function (Result) {
                var data = Result.data;
                if(Result.result == 0){
                    alert(Result.message);
                    window.location.href="login.html"
                }
                else if(Result.result == 2){
                    alert("请完善资料");
                    $("#Modal1").modal("show");
                }
                else{
                    $("#msg_uname").html(data.username);
                    $("#msg-real").html("真实姓名："+data.realName);
                    $("#msg-score").html("用户积分："+data.score);
                    $("#msg-mobile").html("手机号码："+data.mobile);
                    $("#msg-mail").html("邮箱："+data.email);
                    $("#msg-address").html("地址："+data.address);
                    $("#imghead").attr("src","upload/image/"+data.touxiang);
                    $("#pcc_name1").val(data.username);
                    $("#pcc_truename").val(data.realName);
                    $("#pcc_email").val(data.email);
                    $("#pcc_address").val(data.address);
                    $("#pcc_phone").val(data.mobile);
                }
            }
        });
    	
    }
function issign(){
	 $.ajax({
	        type : "post",
	        url : linkUrl + "isSigned",
	        data: {},
	        dataType: 'json',
	        success:function (Result) {
	            var result = Result.result;
	            if(result==1){
	                $("#pcc-qiandao").text('已签到');
	                $("#pcc-qiandao").addClass('pcc-yiqiandao');
	                $(".pcc_dondianji").css("display","block");
	            } 
	        }
	    })
}

// 用户名判断
function pcc_name(i){
		if(i.val()==""){
			i.next().removeClass('hide');
			return false;
		}else{
			i.next().addClass('hide');
			return true;
		}
	}
// 用户密码判断
function pcc_passworder(i){
		if(i.val()==""||!/[A-Za-z0-9]{6,12}$/.test(i.val())){
			i.next().removeClass('hide');
			return false;
		}else{
			i.next().addClass('hide');
			return true;
		}
	}
//手机号码判断
function pcc_phone(i){
		if(i.val()==""||!/^[1]([3][0-9]{1}|[5][0-9]{1}|[8][0-9]{1})[0-9]{8}$/.test(i.val())){
			i.next().removeClass('hide');
			return false;
		}else{
			i.next().addClass('hide');
			return true;
		}
	}
// 邮箱判断
function pcc_email(i){
		if(i.val()==""||!/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test(i.val())){
			i.next().removeClass('hide');
			return false;
		}else{
			i.next().addClass('hide');
			return true;
		}
	}
//手机号修改第一步验证
function pcc_phonexiugai(){//成功后弹窗下一个框
	if($('#pcc_name2').val()==""){
		$('#pcc_name2').next().removeClass('hide');
		return false;
	}else{
		$('#pcc_name2').next().addClass('hide');
	}
   	if($('#pcc_passworder2').val()==""||!/[A-Za-z0-9]{6,12}$/.test($('#pcc_passworder2').val())){
		$('#pcc_passworder2').next().removeClass('hide');
		return false;
	}else{
		$('#pcc_passworder2').next().addClass('hide');
	}
    $.ajax({
        type: 'POST',
        url : linkUrl + "regUser",
        data: {username : $('#pcc_name2').val(),password:$('#pcc_passworder2').val()},
        dataType: 'json',
        success:function (Result) {
            var result = Result.result;
            if(result == -1){
                alert(Result.message);
                window.location.href = "login.html";
            }
            if(result == 0){
                alert(Result.message);
            }
            else{
                $('#Modal31').modal('show');
                $('#Modal3').modal('hide');
            }
        }
    })
}
function pcc_phonebaocun(){
	if($("#pcc_phone2").val()==""||!/^[1]([3][0-9]{1}|[5][0-9]{1}|[8][0-9]{1})[0-9]{8}$/.test($("#pcc_phone2").val())){
		$("#pcc_phone2").next().removeClass('hide');
		return false;
	}else{
		$("#pcc_phone2").next().addClass('hide');
	}
	if($('#pcc_redpassworder2').val()==""||!/[A-Za-z0-9]{6,12}$/.test($('#pcc_redpassworder2').val())){
		$('#pcc_redpassworder2').next().removeClass('hide');
		return false;
	}else{
		$('#pcc_redpassworder2').next().addClass('hide');
	}
    $.ajax({
        type: 'POST',
        url : linkUrl + "regCode",
        data: {mobile : $('#reg_phone2').val(),code:$('#pcc_redpassworder2').val(),type : "change"},
        dataType: 'json',
        success:function (Result) {
            var result = Result.result;
            if(result == 0){
                alert(Result.message);
            }
            else{
                alert(Result.message);
                $('#Modal31').modal('hide');
                window.location.href = "home.html";
            }
        }
    })

}


// 修改密码
function pcc_passworderxiugai(){
  	if($('#pcc_redpassworder').val()==""||!/[0-9]{6}$/.test($('#pcc_redpassworder').val())){
		$('#pcc_redpassworder').next().removeClass('hide');
		return false;
	}else{
		$('#pcc_redpassworder').next().addClass('hide');
	}
    $.ajax({
        type: 'POST',
        url : linkUrl + "regCode",
        data: {mobile : $('#pcc_phone').val(),code:$('#pcc_redpassworder').val(),type : "modify"},
        dataType: 'json',
		success:function (Result) {
			var result = Result.result;
			if(result == 0){
				alert(Result.message);
			}
			else{
                $('#Modal22').modal('show');
                $('#Modal21').modal('hide');
			}
        }
	})
	 //$('#pcc_passworderxiugai').html("<button class='btn btn-info' data-dismiss='modal' data-toggle='modal' data-target='#Modal22'>下一步</button>")

}
function pcc_mimabaocun(){
	if($('#pcc_passworder').val()==""||!/[A-Za-z0-9]{6,12}$/.test($('#pcc_passworder').val())){
		$('#pcc_passworder').next().removeClass('hide');
		return false;
	}else{
		$('#pcc_passworder').next().addClass('hide');
	}
	if ($('#pcc_againpassworder').val()!=$('#pcc_passworder').val()) {
		$('#pcc_againpassworder').next().removeClass('hide');
		return false;
	}
	$.ajax({
        type: 'POST',
        url : linkUrl + "modifyPassword",
        data: {password : $('#pcc_passworder').val()},
        dataType: 'json',
        success:function (Result) {
            var result = Result.result;
            alert(Result.message);
            if(result == -1){
                window.location.href = "login.html";
            }
            else if(result == 1){
                $('#Modal22').modal('hide');
            }
        }
	})
	
}
